<?php
/**
 * Created by PhpStorm.
 * User: 毛文科
 * Date: 2018\10\28 0028
 * Time: 10:03
 */
namespace app\admin\model;
use Qiniu\Auth;
use Qiniu\Storage\BucketManager;
use Qiniu\Storage\UploadManager;
use tegic\qiniu\Qiniu;
use think\Db;

//use tegic\qiniu\Qiniu;
require_once '../vendor/teg1c/thinkphp-qiniu-sdk/qiniu_php_driver/autoload.php';
class Qinu{
    /**删除文件
     * @param $name:要删除的文件名称
     * @return bool:删除成功（true）和删除失败（false）
     */
    public static function DeleFile($name)
    {
        if (\config('use_qiniou')) {
            $name = explode('/',$name);
            $name = end($name);
            $ak = \config('qnAK');
            $sk = \config('qnSK');
            $bucket = \config('qnbucket');
            $auth = new Auth($ak,$sk);
            $bucketManager = new BucketManager($auth);
            $err = $bucketManager->delete($bucket, $name);
            if ($err) {
                return false;
            } else {
                return true;
            }
        }else{
            if (is_file('.'.$name)){
                unlink('.'.$name);
            }
            return true;
        }
    }
    public static function DeleFileForId($id){
        $name = Db::name('enclosure')
            ->where('id',$id)
            ->find();
        if (\config('use_qiniou')) {
            $name = $name['name'];
            $ak = \config('qnAK');
            $sk = \config('qnSK');
            $bucket = \config('qnbucket');
            $auth = new Auth($ak,$sk);
            $bucketManager = new BucketManager($auth);
            $err = $bucketManager->delete($bucket, $name);
            if ($err) {
                return false;
            } else {
                return true;
            }
        }else{
            $name = $name['path'];
            if (is_file('.'.$name)){
                unlink('.'.$name);
            }
            return true;
        }
    }

    /**将文件保存的心方法
     * @param $file:文件
     * @param bool $bool:是否重命名
     * @param array $type:文件类型
     * @param null $size:文件大小
     * @return array|bool|string
     */
    static public function Upfile($file,$bool=true,$type='all',$size=200*1000*1000){
        if(!$file){
            return false;
        }
        $data = [];
        if(is_array($file)){
            foreach ($file as $value){
            $data[] = Qinu::Files($value,$bool,$type,$size);
            }
        }else{
            $data = Qinu::Files($file,$bool,$type,$size);
        }
        return $data;
    }

    /**获取键名的上传图片
     * @param $file
     * @param bool $bool
     * @param string $type
     * @param int $size
     * @return array
     */
    static  public function GetKeyUpfile($file,$bool=true,$type='all',$size=200*1000*1000){
        $data = [];
        foreach ($file as $key=>$value){
            $data[$key] = Qinu::Upfile($value,$bool=true,$type='all',$size=200*1000*1000);
        }
        return $data;
    }

    /**添加文件
     * @param $files
     * @param bool $bool
     * @param string $type
     * @param int $size
     * @return bool|mixed|string
     */
    private static function Files($files,$bool=true,$type='all',$size=200*1000*1000){
        if(\config('use_qiniou')){
            return Qinu::Filess_qiou($files,$bool=true,$type='all',$size=200*1000*1000);
        }else{
            return Qinu::Filess_ben($files,$bool=true,$type='all',$size=200*1000*1000);
        }
    }

    /**数据添加在七牛云
     * @param $files
     * @param bool $bool
     * @param string $type
     * @param int $size
     * @return bool|string
     */
    private static function Filess_qiou($files,$bool=true,$type='all',$size=200*1000*1000){
        $file = $files->getInfo();
        if($bool){
            $array = explode('.',$file['name']);
            $name = uniqid().mt_rand(1000,9999).'.'.$array[1];
        }else{
            $name = $file['name'];
        }
        if(!$type=='all'){
            $array = explode('/',$file['type']);
            if(in_array($array[1],$type)){
                return false;
            }
        }
        if($file['size']>$size){
            return false;
        }
//        echo '<pre>';print_r($file);exit;
        $ak = \config('qnAK');
        $sk = \config('qnSK');
        $bucket = \config('qnbucket');
        $auth = new Auth($ak, $sk);
        $token = $auth->uploadToken($bucket);
        $uploadMgr = new UploadManager();
        $filePath = $file['tmp_name'];
        list($ret,$err) = $uploadMgr->putFile($token,$name,$filePath);
        if ($err !== null) {
            return false;
        } else {
            return config('qiniou_url').$name;
        }
    }

    /**文件添加在本服务器
     * @param $files
     * @param bool $bool
     * @param string $type
     * @param int $size
     * @return mixed
     */
    private static function Filess_ben($files,$bool=true,$type='all',$size=200*1000*1000){
        $file = $files->getInfo();
        if(!$type=='all'){
            $info = $files->validate(['size'=>$size,'type'=>$type])->move('.'.\config('fuwuqi'));
            return config('fuwuqi').'/'.str_replace("\\","/",$info->getSaveName());
        }else{
            $info = $files->validate(['size'=>$size])->move('.'.\config('fuwuqi'));
            return config('fuwuqi').'/'.str_replace("\\","/",$info->getSaveName());
        }
    }
}